/**
 * Created by Administrator on 2015/6/5.
 */
var redis=require('redis');
var lineReader=require('line-reader');
var queryString=require('querystring');
var cheerio=require("cheerio");
var http = require('http');
var iconv = require('iconv-lite');
var BufferHelper = require('bufferhelper');
var httpTools=require('./HttpTools');
try { var unzip = require('zlib').unzip } catch(e) { /* unzip not supported */ }
var importIps=function(){
    var self=this;
    var settings=require('./settings');
    self.redis_cli=redis.createClient(
        settings['proxy_info_redis_db'][1],
        settings['proxy_info_redis_db'][0]
    );
    self.redis_cli.on('error',function(error){
        console.error(error);
    });
    self.redis_cli.select(settings['proxy_info_redis_db'][2],function(err,value){
        //self.redis_cli=redis_cli;
    });
    var pageLink='http://www.xici.net.co/nt';
    var options=httpTools.requestOption(pageLink,'GET','http://www.xici.net.co/');
    options['headers']['Cookie']='CNZZDATA4793016=cnzz_eid%3D863874188-1433911920-null%26ntime%3D1434337209';
    var request=http.request(options,function(res){
        var compressed = /gzip|deflate/.test(res.headers['content-encoding']);
        var bufferHelper = new BufferHelper();
        res.on('data', function (chunk) {
            bufferHelper.concat(chunk);
        });
        res.on('end', function (chunk) {
            var result='';
            var page_encoding = httpTools.get_page_encoding(res.headers);
            page_encoding = page_encoding.toLowerCase().replace('\-','')
            if(!compressed || typeof unzip == 'undefined'){
               result=iconv.decode(bufferHelper.toBuffer,page_encoding);
                this.ipanalyse(result);
            }else{
                unzip(bufferHelper.toBuffer(), function(err, buff) {
                    if (!err && buff) {
                        result=iconv.decode(buff,page_encoding);
                        self.ipanalyse(result);
                    }else{

                    }
                });
            }


        });
    });
    request.on('error',function(err){
        if(err) {
            console.error(err);
            process.exit(1);
        }
    });
    request.end();

    //lineReader.eachLine("./instance/abc/logs/proxyip.txt",function(line,last){
    //     console.info(last+"======="+line);
    //     this.redis_cli.lpush('proxy:public:available:3s',line,function(err,data){
    //         //console.info(data);
    //     });
    //});
};
importIps.prototype.ipanalyse=function(data){
    var cli=this;
    var $= cheerio.load(data);
    $('#ip_list tr').each(function(index,el){
        if($(el).attr('class')!==undefined){
            var tds=$(el).find('td');
            if($(tds[6]).text()==='HTTP'){
                var  ip=$(tds[2]).text();
                var  port=$(tds[3]).text();
                ip=ip+":"+port;
                cli.redis_cli.lpush('proxy:public:available:3s',ip,function(err,data){
                    console.info(data);
                });
            }
        }
    });
};
//var im=new  importIps();
var postTest= function () {
    var data={a:2,b:3,c:4};
    var pageLink='http://localhost:8080/hello';
    var options = {
        hostname: '127.0.0.1',
        port: 8080,
        path: '/hello',
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
        }
    };
    //var options=httpTools.requestOption(pageLink,'GET');
  var request=http.request(options,function(res){

  });
  request.on('error',function(err){
      console.error(err);
  });
  request.write(queryString.stringify(data));
  request.end();
};
postTest();